package com.android.ailaoshan.sound;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.ailaoshan.R;
import com.android.ailaoshan.common.BaseApplication;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.io.File;
import java.util.ArrayList;


/**
 * 录音界面
 */
public class SoundRecorderActivity extends AppCompatActivity {

    private Context mContext;

    private Toolbar toolbar;
    private Chronometer timerTxt;
    private TextView statusTxt;
    private ImageButton controlBut;
    private Button giveUpBtn;
    private Button saveBtn;

    /**
     * 是否开始
     */
    private boolean isStart = false;

    private StartSoundRecorder startSoundRecorder;
    private BaseApplication baseApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sound_recorder_activity);

        mContext = SoundRecorderActivity.this;

        baseApplication = BaseApplication.getInstance();
        baseApplication.addActivity(SoundRecorderActivity.this);

        toolbar = findViewById(R.id.sound_recorder_toolbar);
        timerTxt = findViewById(R.id.sound_recorder_time);
        statusTxt = findViewById(R.id.sound_recorder_status);
        controlBut = findViewById(R.id.sound_recorder_controller);
        giveUpBtn = findViewById(R.id.sound_recorder_giveUp);
        saveBtn = findViewById(R.id.sound_recorder_save);
    }

    @Override
    protected void onResume(){
        super.onResume();

        startSoundRecorder = new StartSoundRecorder(mContext,timerTxt);

        controlBut.setOnClickListener(
           (v) -> {
                if(!isStart){
                    startSoundRecorder.startRecorder();
                    statusTxt.setText("正在录音");
                    controlBut.setImageDrawable(mContext.getResources().getDrawable(R.drawable.recorder_stop));
                    isStart = true;
                }else{
                    startSoundRecorder.stopRecorder();
                    statusTxt.setText("录音停止");
                    controlBut.setImageDrawable(mContext.getResources().getDrawable(R.drawable.recorder_start));
                    isStart = false;

                    // 隐藏控制按钮，显示选择按钮
                    controlBut.setVisibility(View.INVISIBLE);
                    giveUpBtn.setVisibility(View.VISIBLE);
                    saveBtn.setVisibility(View.VISIBLE);
                }
            }
        );

        // 放弃保存
        giveUpBtn.setOnClickListener(
            (v) -> {
                if(startSoundRecorder != null){
                    startSoundRecorder.deleteFile();

                    // 显示控制按钮
                    giveUpBtn.setVisibility(View.INVISIBLE);
                    saveBtn.setVisibility(View.INVISIBLE);
                    controlBut.setVisibility(View.VISIBLE);

                    startSoundRecorder.resetTimer();
                    statusTxt.setText("");
                }
            }
        );

        // 保存文件并退出
        saveBtn.setOnClickListener(
            (v) -> {
                String myFilePath = baseApplication.getVoiceFile();
                File soundFile = new File(myFilePath);
                if(soundFile.exists()){
                    ArrayList<String> dataList = new ArrayList<>();
                    dataList.add(soundFile.getName());

                    Intent intent = new Intent(SoundRecorderActivity.this,SoundActivity.class);
                    intent.putStringArrayListExtra("soundList",dataList);
                    intent.putExtra("saveType",1);
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext,"文件不存在或已损坏",Toast.LENGTH_SHORT).show();
                }
                exitActivity();
            }
        );

        // 点击了退出
        toolbar.setNavigationOnClickListener(
            (v) -> {
                if(isStart){
                    showExitMessage();
                }else{
                    exitActivity();
                }
            }
        );
    }

    /**
     * 退出提示框
     */
    private void showExitMessage(){
        new QMUIDialog.MessageDialogBuilder(SoundRecorderActivity.this)
                .setTitle("退出")
                .setMessage("现在正在录音，退出后录音文件不能保存，确定退出吗？")
                .addAction("取消",new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                }).addAction(0,"退出",QMUIDialogAction.ACTION_PROP_NEGATIVE,new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        if(startSoundRecorder != null){
                            startSoundRecorder.stopRecorder();
                            startSoundRecorder.deleteFile();

                            exitActivity();
                        }
                        dialog.dismiss();
                    }
                }).create(R.style.QMUI_Dialog).show();
    }

    /**
     * 退出应用
     */
    private void exitActivity(){
        if(baseApplication != null){
            baseApplication.removeActivity(SoundRecorderActivity.this);
        }else{
            finish();
        }
    }

    /**
     * 点击了返回
     */
    @Override
    public void onBackPressed(){
        if(isStart){
            showExitMessage();
        }else{
            exitActivity();
        }
    }

    @Override
    public void onDestroy(){
        super.onDestroy();

        mContext = null;
        startSoundRecorder = null;
        baseApplication = null;
    }
}
